/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#ifndef __DATAMODEL_H__
#define __DATAMODEL_H__

#include <QObject>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QTextEdit>
#include <QStatusBar>
#include <QDateTime>

#include <QDomDocument>

class ModelItem : private QObject
{
	Q_OBJECT
public:
	ModelItem(QString name, QString type, QString data, QString tags, QDateTime creationDate);	
			
	QString itemName;
	QString itemType;
	QString itemData;
	QString itemTags;
	QDateTime itemCreationDate;
};

class Model : public QObject
{
	Q_OBJECT
public:
	Model(QTreeWidget *treeWidget, QTextEdit *editor, QStatusBar *statusBar, QString fileName, QString dateFormat);
	~Model();
				
	void changeName(int index, QString newName);
	int addItem(QString name, QString type, QString data, QString tags, QDateTime creationDate);
	QString getName(int index);
	QString getType(int index);
	QString getData(int index);
	QString getTags(int index);
	QDateTime getCreationDate(int index);
	
	void updateTags(int index, QString tags);
	
	ModelItem* getModelItem(int index);
	int getSize();
	
	bool askForSaveAndSave(QWidget *parent);
	void save();
	void saveAs(QString fileName);
	bool open();
	
	void updateDisplay(QTreeWidgetItem *newItem);	
	void updateModelWithCurrent();
	
	QString getFileName();
	QTreeWidgetItem * getCurrentItem();
	void setCurrentItem(QTreeWidgetItem *value);
	void setFileModified(bool value);
	
	void setDateFormat(QString value);
	
	QIcon m_folderIcon;
	QIcon m_bookmarkIcon;
	
	bool isNagainaDocument();
	QString getDocumentVersion();

private:
	QList<ModelItem *> *list;
	
	QTreeWidget *m_treeWidget;
	QTreeWidgetItem *m_currentItem;
	bool m_boFileModified;
	QString m_stFileName;
	QString m_stDateFormat;
	QTextEdit *m_editor;
	QStatusBar *m_statusBar;
	QDateTime m_fileCreationDate;
	
	bool m_boNagainaDocument;
	QString m_stDocumentVersion;
	
	void updateData(int index, QString data);
	void clear();
	bool checkFileModified();
	
	QString getCategoryHtml(QTreeWidgetItem *item);
	
	bool readFile();
	void ParseElement(const QDomElement &element, QTreeWidgetItem *parentItem, QTreeWidget *treeWidget);		
	
	void writeFile();
	void processSaveItem(QDomDocument *doc, QDomElement *parent, QTreeWidgetItem *item);	
};

#endif // __DATAMODEL_H__
